package com.shuaitao.newsystem.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Component
public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
//        response.sendRedirect("/fdsafasf");
        Map<String, Object> data = new HashMap<>();
        response.setContentType("application/json;charset=UTF-8");
        data.put("msg","登陆已过期或未登录，请登录！！！");
        data.put("code","101");
        response.setStatus(200);
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonData = objectMapper.writeValueAsString(data);
//        response.addHeader("msg","请先登录!!!");
        response.getWriter().write(jsonData);
    }
}
